<template>
  <div style="display: flex;">
    <div class="color">
      <div class="box" :style="{ backgroundColor: rgbColor }"></div>
      <div class="control">
        <div v-for="(x, y) in names" :key="y">
          <div>{{ x }}</div>
          <div>
            <el-slider v-model="state.colors[y]" max="255" />
          </div>
          <div>
            <el-input v-model="state.colors[y]" disabled></el-input>
          </div>
        </div>
      </div>
      <div class="name">
        <div>{{ rgbColor }}</div>
        <div>{{ hexColor }}</div>
      </div>
      <div class="btn">
        <div @click="randomColor"><el-button> 随机色 </el-button></div>
        <div @click="copyRgb"><el-button> 复制RGB </el-button></div>
        <div @click="copyHex"><el-button> 复制HEX </el-button></div>
      </div>
    </div>
    <!-- <div class="rgb">
      <div v-for="x in 255" :key="x">
        <div v-for="a in 255" :key="x" :style="{ background: 'rgb(' + x + ',' + a + ',255)' }">

        </div>
      </div>
    </div> -->
  </div>
</template>

<script setup>
import { reactive, computed } from 'vue';
import element  from '@/utils/element';
const names = ['R', 'G', 'B']
const state = reactive({
  rgb: '',
  hex: '',
  colors: [
    0, 0, 0
  ]
})

const rgbColor = computed(() => {
  return 'rgba(' + state.colors.join(',') + ')'
})

const hexColor = computed(() => {
  return '#' + state.colors.map(x => {
    const hex = x.toString(16);
    return hex.length === 1 ? '0' + hex : hex;
  }).join('');
})

function mathFloor() {
  return Math.floor(Math.random() * 256) // 生成0到255之间的随机数
}

function randomColor() {
  state.colors[0] = mathFloor()
  state.colors[1] = mathFloor()
  state.colors[2] = mathFloor()
}
function copyRgb() {
  // console.log(rgbColor.value)
  navigator.clipboard.writeText(rgbColor.value).then(() => {
    element.success('复制成功')
  })
}
function copyHex() {
  // console.log(hexColor.value)
  navigator.clipboard.writeText(hexColor.value).then(() => {
    element.success('复制成功')
  })
}
</script>

<style scoped lang="scss">
.color {
  width: 320px;
  margin: 20px;
  padding: 10px;
  border: 1px solid #fff;

  .box {
    width: 100px;
    height: 300px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .name{
    display: flex;
    >div{
      width: 50%;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .control {
    width: 100%;

    >div {
      display: flex;
      margin: 10px;

      >div:nth-child(1) {
        width: 40px;
        text-align: center;
      }

      >div:nth-child(2) {
        width: 180px;
        margin-right: 20px;
      }

      >div:nth-child(3) {
        width: 50px;
      }
    }
  }

  .btn {
    display: flex;

    >div {
      margin-right: 10px;
    }
  }
}

.rgb {
  width: 255px;

  >div {
    display: flex;
    width: 255px;
    height: 1px;

    >div {
      width: 1px;
      height: 1px;
    }
  }
}
</style>
